﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace T.FAS.Runtime.MF.Entity
{
    /// <summary>
    /// SG接口返回的结果
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    [DataContract]
    public class SGResult<T> //where T:class
    {
        /// <summary>
        /// 
        /// </summary>
        private string code;
        private T data;
        private string message = string.Empty;
        private Exception exception = null;

        [DataMember(Name="state")]
        public string Code
        {
            get { return code;}
            set { code = value; }
        }
        [DataMember(Name = "data")]
        public T Data
        {
            get { return data; }
            set { data = value; }
        }
        [DataMember(Name = "errmsg")]
        public String Message
        {
            get { return message; }
            set { message = value; }
        }
        [DataMember (Name ="exception")]
        public Exception Exception
        {
            get { return exception; }
            set { exception = value; }
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        /// <param name="data">待返回的数据</param>
        /// <returns></returns>
        public static SGResult<T> Success(T data)
        {
            SGResult<T> result = new SGResult<T>();
            result.code = "1";
            result.data = data;
            result.message = "操作成功";
            return result;
        }

        /// <summary>
        /// 操作失败
        /// </summary>
        /// <param name="code">错误码，1:表示成功，非1表示失败</param>
        /// <param name="ex">异常消息</param>
        /// <returns></returns>
        public static SGResult<T> Fail(string code, Exception ex = null, string message = "操作失败")
        {
            SGResult<T> result = new SGResult<T>();
            result.code = code;
            result.data = default(T);
            result.message = message;
            result.exception = ex;
            return result;
        }
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsOK { get { return code == "1"; } }

    }
}
